package servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet("*.user")
public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String servletPath = request.getServletPath();
        if(servletPath.contains("regist.user"))
            regist(request,response);
        if(servletPath.contains("login.user"))
            login(request,response);
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            String url = "jdbc:mysql://127.0.0.1:3306/ums?useUnicode=true&characterEncoding=utf-8";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "");
            String sql = new StringBuffer()
                    .append("select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .append(" where username = ?").toString();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,username);
            rs = pstmt.executeQuery();
            if(rs.next())
            {
                String pwd = rs.getString("password");
                System.out.println(pwd+" === "+ password);

                if(!pwd.equals(password)) {
                    request.setAttribute("loginMsg", "密码不正确");
                    request.getRequestDispatcher("/login.jsp").forward(request,response);
                }else {
                    request.getSession().setAttribute("username", username);
                    response.sendRedirect(request.getContextPath() + "/firstpage.jsp");
                }
            }else
            {
                request.setAttribute("loginMsg","用户名不存在");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("registMsg","服务器异常");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }

    private void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            String url = "jdbc:mysql://127.0.0.1:3306/ums?useUnicode=true&characterEncoding=utf-8";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "");
            String sql = new StringBuffer()
                    .append("select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .append(" where username = ?").toString();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,username);
            rs = pstmt.executeQuery();
            if(rs.next())
            {
                request.setAttribute("registMsg","该用户已被注册");
                request.getRequestDispatcher("/regist.jsp").forward(request,response);
            }

            sql = new StringBuffer()
                    .append("insert into t_user ")
                    .append(" (username,password,phone,address) ")
                    .append(" values ")
                    .append(" (?,?,?,?)").toString();
            pstmt  = conn.prepareStatement(sql);
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            pstmt.setString(3,phone);
            pstmt.setString(4,address);
            pstmt.executeUpdate();
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("registMsg","服务器异常");
            request.getRequestDispatcher("/regist.jsp").forward(request,response);
        }
    }
}
